<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

if(isset($_GET["id"])) {
    if(!Check::isNum($_GET["id"])||(int)$_GET["id"]<=0){
        exit(Result::jsonReturn(1, "参数有误"));
    }
    $db = new Db();
    $id = (int)$_GET["id"];
    $sql = "SELECT id,slug,name,description,type,created_time,updated_time FROM qilian_lists WHERE id={$id} AND type=0";
    $res = $db->getOne($sql);
    if($res) {
      $sql = "SELECT c.id,c.title,c.slug,c.type,c.created_time,c.updated_time FROM qilian_contents as c ".
             "LEFT JOIN qilian_relationships as r ON c.id=r.content_id WHERE r.list_id={$id} AND c.type=0";
      $res_contents = $db->getAll($sql);
      if($res_contents) {
        $res['contents'] = $res_contents;
        exit(Result::jsonReturn(0, "获取数据成功", $res));
      }else {
        exit(Result::jsonReturn(1, "获取失败"));
      }
    }else {
      exit(Result::jsonReturn(1, "获取失败"));
    }
}elseif(isset($_GET["slug"])) {
  if(!Check::isSlug($_GET["slug"])) {
    exit(Result::jsonReturn(1, "参数有误"));
  }
  $db = new Db();
  $slug = $_GET["slug"];
  $sql = "SELECT id,slug,name,description,type,created_time,updated_time FROM qilian_lists WHERE slug='{$slug}' AND type=0";
  $res = $db->getOne($sql);
  if($res) {
    $id = (int)$res["id"];
    $sql = "SELECT c.id,c.title,c.slug,c.type,c.created_time,c.updated_time FROM qilian_contents as c ".
           "LEFT JOIN qilian_relationships as r ON c.id=r.content_id WHERE r.list_id={$id} AND c.type=0";
    $res_contents = $db->getAll($sql);
    foreach($res_contents as $index=>$val) {
      $id = (int)$val["id"];
      $sql = "SELECT title FROM qilian_attachments WHERE content_id={$id}";
      $res_attachments = $db->getAll($sql);
      $res_contents[$index]["attachments"] = array();
      foreach($res_attachments as $val_in) {
        array_push($res_contents[$index]["attachments"], CDN.$val_in["title"]);
      }
    }
    $res['contents'] = $res_contents;
    exit(Result::jsonReturn(0, "获取数据成功", $res));
  }else {
    exit(Result::jsonReturn(1, "获取失败"));
  }
}else {
    exit(Result::jsonReturn(1, "缺少参数"));
}

?>